package com.netx.generics.util;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;

import com.netx.generics.basic.Checker;


public class Strings {

	private Strings() {
		super();
	}

	public static boolean isEmpty(String s) {
		return s==null || s.trim().equals("");
	}

	public static boolean isUpperCase(String s) {
		Checker.checkNull(s, "s");
		for(int i=0; i<s.length(); i++) {
			if(Character.isLowerCase(s.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static boolean isLowerCase(String s) {
		Checker.checkNull(s, "s");
		for(int i=0; i<s.length(); i++) {
			if(Character.isUpperCase(s.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean isInteger(String s) {
		Checker.checkEmpty(s, "s");
		try {
			new Integer(s);
			return true;
		}
		catch(NumberFormatException nfe) {
			return false;
		}
	}

	public static boolean isNumber(String s) {
		Checker.checkEmpty(s, "s");
		try {
			new Double(s);
			return true;
		}
		catch(NumberFormatException nfe) {
			return false;
		}
	}

	public static String addSlashes(String s) {
		Checker.checkNull(s, "s");
		StringBuilder b = new StringBuilder(s);
		for(int i=0; i<b.length(); i++) {
			if(b.charAt(i) == '"' || b.charAt(i) == '\'' || b.charAt(i) == '\\') {
				b.insert(i, '\\');
				i++;
			}
		}
		return b.toString();
	}

	public static String valueOf(int i, int length) {
		return valueOf(new Integer(i), length);
	}

	public static String valueOf(Integer i, int length) {
		Checker.checkNull(i, "i");
		Checker.checkIndex(length, "length");
		StringBuilder result = new StringBuilder(i.toString());
		if(result.length() < length) {
			for(int j=result.length(); j<length; j++) {
				result.insert(0, '0');
			}
		}
		return result.toString();
	}

	public static String valueOf(double d, int l, int r) {
		return valueOf(new Double(d), l, r);
	}

	public static String valueOf(Double d, int llength, int rlength) {
		Checker.checkNull(d, "d");
		Checker.checkIndex(llength, "llength");
		Checker.checkIndex(rlength, "rlength");
		String number = d.toString();
		StringBuilder result = new StringBuilder(number);
		String[] array = number.split("[.]");
		if(llength != 0) {
			if(array[0].length() < llength) {
				for(int j=array[0].length(); j<llength; j++) {
					result.insert(0, '0');
				}
			}
		}
		if(rlength != 0) {
			if(array[1].length() > rlength) {
				return result.substring(0, result.indexOf(".")+rlength+1);
			}
			else if(array[1].length() < rlength) {
				for(int j=array[1].length(); j<rlength; j++) {
					result.append('0');
				}
			}
		}
		return result.toString();
	}

	public static int countOccurrences(String s, char toCount) {
		Checker.checkNull(s, "s");
		int count = 0;
		for(int i=0; i<s.length(); i++) {
			if(s.charAt(i) == toCount) {
				count++;
			}
		}
		return count;
	}
	
	public static int countOccurrences(String s, String toCount) {
		Checker.checkNull(s, "s");
		Checker.checkNull(toCount, "toCount");
		int count = 0;
		StringBuilder sb = new StringBuilder(s);
		while(true) {
			int index = sb.indexOf(toCount);
			if(index == -1) {
				return count;
			}
			else {
				count++;
				// TODO more efficient if we use sb.indexOf(toCount, lastIndex)
				sb.replace(index, index+toCount.length(), "");
			}
		}
	}
	
	public static String repeat(String s, int numTimes) {
		Checker.checkNull(s, "s");
		Checker.checkMinValue(numTimes, 1, "numTimes");
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<numTimes; i++) {
			sb.append(s);
		}
		return sb.toString();
	}
	
	public static String replaceAll(String s, String toReplace, String replacement) {
		Checker.checkEmpty(s, "s");
		Checker.checkEmpty(toReplace, "toReplace");
		replacement = replacement == null  ? "" : replacement;
		StringBuilder sb = new StringBuilder(s);
		for(int index = sb.indexOf(toReplace); index != -1; index = sb.indexOf(toReplace)) {
			sb.replace(index, index+toReplace.length(), replacement);
		}
		return sb.toString();
	}
	
	public static String eliminateRepeatedChars(String s) {
		Checker.checkEmpty(s, "s");
		Set<Character> tmp = new HashSet<Character>();
		for(int i=0; i<s.length(); i++) {
			tmp.add(s.charAt(i));
		}
		StringBuilder sb = new StringBuilder();
		Iterator<?> it = tmp.iterator();
		while(it.hasNext()) {
			sb.append(it.next());
		}
		return sb.toString();
	}
}
